package com.klz.iblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.klz.iblog.common.Constants;
import com.klz.iblog.common.Result;
import com.klz.iblog.common.ResultGenerator;
import com.klz.iblog.pojo.Blog;
import com.klz.iblog.service.BlogService;
import com.klz.iblog.vo.ArchiveInfo;
import com.klz.iblog.vo.ArchivePageInfo;
import com.klz.iblog.vo.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags={"blog接口"})
public class BlogController {

    @Autowired
    private BlogService blogService;

    @ApiOperation(value = "查询博客的所有点赞数")
    @GetMapping("/selectAllLikeNumCounts")
    public Result<Integer> selectAllLikeNumCounts() {
        try{
           return ResultGenerator.genSuccessResult(blogService.selectAllLikeNumCounts());
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation(value = "查询博客的所有浏览量")
    @GetMapping("/selectAllBrowseNumCounts")
    public Result<Integer> selectAllBrowseNumCounts() {
        try{
            return ResultGenerator.genSuccessResult(blogService.selectAllBrowseNumCounts());
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("查询所有博客的总数")
    @GetMapping("/selectAllBlogCount")
    public Result<Integer> selectAllBlogCount() {
        try{
            return ResultGenerator.genSuccessResult(blogService.selectAllBlogCount());
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询博客信息")
//    @ApiImplicitParam(name = "PageUtil", value = "页面工具类", required = true, dataType = "PageUtil", paramType = "jquery")
    @GetMapping("/selectBlogInfo")
    public Result<Blog> selectPageBlogInfo(@RequestBody PageUtil pageUtil) {

        if(StringUtils.isEmpty(pageUtil.getPage()) || StringUtils.isEmpty(pageUtil.getLimit())) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR,"参数异常");
        }

        try{
            PageHelper.startPage(pageUtil.getPage(), pageUtil.getLimit());
            List<Blog> blogList = blogService.selectPageBlogInfo();
            PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogList);
            return ResultGenerator.genSuccessResult(blogList);
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation(value = "按浏览量分页查询博客信息")
//    @ApiImplicitParam(name = "PageUtil", value = "页面工具类", required = true, dataType = "PageUtil", paramType = "body")
    @GetMapping("/selectBlogInfoByBrowseNum")
    public Result<Blog> selectPageBlogInfoByBrowseNum(@RequestBody PageUtil pageUtil) {

        if(StringUtils.isEmpty(pageUtil.getPage()) || StringUtils.isEmpty(pageUtil.getLimit())) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR,"参数异常");
        }

        try{
            PageHelper.startPage(pageUtil.getPage(), pageUtil.getLimit());
            List<Blog> blogList = blogService.selectPageBlogInfoByBrowseNum();
            PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogList);
            return ResultGenerator.genSuccessResult(blogList);
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @GetMapping("/selectHotBlogsInfo")
    @ApiOperation("查询热门博客的推荐信息")
    public Result<Blog> selectHotBlogs(){
        try{
            return ResultGenerator.genSuccessResult(blogService.selectHotBlogs(Constants.HOT_BLOG_LIMIT_NUM));
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }

    }

    @GetMapping("/selectNewBlogsInfo")
    @ApiOperation("查询最新博客的推荐信息")
    public Result<Blog> selectNewBlogs() {
        try{
            return ResultGenerator.genSuccessResult(blogService.selectNewBlogs(Constants.NEW_BLOG_LIMI_TNUM));
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("根据博客id,查询博客的详细信息")
//    @ApiImplicitParam(name = "blogId", value = "博客id", required = true, dataType = "int", paramType = "query")
    @GetMapping("/selectBlogDetailInfo")
    public Result<Blog> selectBlogDetailInfo(@RequestParam("blogId") Integer blogId) {

        if(StringUtils.isEmpty(blogId)) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_PARAM_ERROR,"参数异常");
        }

        try{
            return ResultGenerator.genSuccessResult(blogService.selectBlogDetailInfo(blogId));
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("查询归档信息")
    @GetMapping("/selectArchivesInfo")
    public Result<ArchiveInfo> selectArchivesInfo() {
        try{
            return ResultGenerator.genSuccessResult(blogService.selectArchivesInfo());
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("按年月分页归档查询博客列表信息")
//    @ApiImplicitParam(name = "ArchivePageInfo", value = "按年月分页查询", required = true, dataType = "ArchivePageInfo", paramType = "body")
    @GetMapping("/selectBlogInfoByArchive")
    public Result<Blog> selectBlogInfoByArchive(@RequestBody ArchivePageInfo archivePageInfo) {
        try{
            PageHelper.startPage(archivePageInfo.getPage(), archivePageInfo.getLimit());
            List<Blog> blogList = blogService.selectBlogInfoByArchive(archivePageInfo.getDate());
            PageInfo<Blog> pageInfo = new PageInfo<Blog>(blogList);
            return ResultGenerator.genSuccessResult(pageInfo);
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }

    @ApiOperation("查询该博客详情和所有评论")
    @GetMapping("/selectBlogAllCommentsInfo")
    public Result<Blog> selectBlogAllCommentsInfo(@RequestParam("blogId") Integer blogId) {
        try{
            return ResultGenerator.genSuccessResult(blogService.selectBlogAllCommentsInfo(blogId));
        }catch (Exception e) {
            return ResultGenerator.genErrorResult(Constants.RESULT_CODE_SERVER_ERROR,"服务器异常-->"+e.getMessage());
        }
    }
}
